<?php

class Controller_Testimonials extends Controller_Website
{
    public function before() {
        parent::before();
        $this->section = 'testimonials';
        $this->mod = Model::factory('directory');
        $this->man = Model::factory('manager');
        $this->itemsPerPage = 10;
        $this->addScript('/scripts/stars/jquery.form.js');
        $this->addScript('/scripts/stars/jquery.MetaData.js');

        $this->addScript('/scripts/cusel/jquery-1.6.1.min.js');
        $this->addScript('/scripts/cusel/cusel-min-2.5.js');
        $this->addStyle('/styles/cusel/cusel.css');

        //auto load when scroll
        $this->addScript('/scripts/autoScroll.js');

        $this->template->activeCat = 'testimonials';
    }
	public function actionIndex($page = 1 , $paginator = null)
	{
        $page = (int)$page - 1;
        if(Request::$isAjax){
//            $page = (int)$page - 1;
            $this->autoRender = false;
            $testimonials = $this->man->getTestimonialsWithLimit($page,$this->itemsPerPage);

            if(isset($testimonials)){
                foreach($testimonials as $testimonial){
                    echo'    <div class="test_item">
                        <div class="test_item_left">
                            <div class="test_item_author">'.$testimonial['name'].'</div>
                            <div class="test_item_rating">Rating
                                <span class="rating_background"></span>
                                <span class="rating_overlay" style="width:'.(int)$testimonial['note1'] * 16 .'px"></span>
                            </div>
                        </div>

                        <div class="test_item_right">
                            <div class="test_item_text">
                            '.$testimonial['text1'].'
                            </div>
                        </div>
                    </div>';
                }
            }
            exit();
        }
		$this->template->title = $this->settings['title'];
		$this->template->content = new View('testimonials/index');

//        $this->template->content->testimonials=$this->man->getTestimonials(5);
        $this->template->content->testimonials=$this->man->getTestimonialsWithLimit($page,$this->itemsPerPage);
        if(isset($_SESSION['identity'])){
            $form = new Form('testimonial');
//            $form->select('point',array('1'=>'1 point','2'=>'2 point'),'','add your point')->rule('required');
            $form->textarea('text1', '')->rule('required')->attribute('placeholder','Are you pleased with our services?');
            $form->select('note1',array(''=>'Add your point','1'=>'1 point','2'=>'2 points','3'=>'3 points','4'=>'4 points','5'=>'5 points'),'Your points')->rule('required');
            $form->submit('submit', 'Publish');
            if (Request::$method == 'POST') {
                if ($form->validate()) {
                    $values = $form->getValues();
                    $values['section'] = $section='indextestimonials';
                    $values['name']=isset($_SESSION['identity']['fullName'])?$_SESSION['identity']['fullName']:$_SESSION['identity']['name'];
                    $alias = Url::safe((isset($form->elements['name']) ? $form->elements['name']->value : ''));
                    $alias = $alias != '' ? $alias : $section;
                    $cnt = 0;
                    Model_Table::instance('directoryItems')->keys['string'] = 'token';
                    while (true === Model_Table::instance('directoryItems')->exists(($token = System::random('alphanuml', 3))) && $cnt < 3) {
                        $cnt++;
                    }
					$values['token'] = $token;
                    $values['alias'] = $alias;
                    $values['parentId'] = $categoryId=0;
                    Model_Table::instance('directoryItems')->insert($values);
                    $this->response->redirect('/'.$this->section.'/');
                }
            }
            $this->template->content->form = $form;
        }
	}
}